package com.system.web.common.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 权限实体，对应于数据库中的权限记录
 * @author <a href="mailto:liushuaiying@139130.net">Shuaiying.Liu</a>
 * @Data 2012-9-5
 * @Version 1.0.0
 */
public class Permission extends BaseEntity implements Serializable {
	private static final long serialVersionUID = -1529450337715510535L;

	private int id;
	private String name;
	private int parentId;
	private String controller;
	private String action;
	private int request;
	private boolean isMenu;
	private boolean isHidden;

	private String ico_cls;
	private int level;
	private String permUrl;
	/** supported data scopes */
	private String dataScopes;
	private int platformId;
	private Platform platform;

	private List<Permission> children;// 子权限列表
	private boolean checked;// 角色当中是否选中
	private int dataScopeId;// 角色当中所选权限范围

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getController() {
		return controller;
	}

	public void setController(String controller) {
		this.controller = controller;
	}

	public int getParentId() {
		return parentId;
	}

	public void setParentId(int parentId) {
		this.parentId = parentId;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public int getRequest() {
		return request;
	}

	public void setRequest(int request) {
		this.request = request;
	}

	public boolean isMenu() {
		return isMenu;
	}

	public void setMenu(boolean isMenu) {
		this.isMenu = isMenu;
	}
	
	public boolean isIsHidden() {
		return isHidden;
	}

	public void setHidden(boolean isHidden) {
		this.isHidden = isHidden;
	}

	public String getIco_cls() {
		return ico_cls;
	}

	public void setIco_cls(String ico_cls) {
		this.ico_cls = ico_cls;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public String getDataScopes() {
		return dataScopes;
	}

	public void setDataScopes(String dataScopes) {
		this.dataScopes = dataScopes;
	}

	public int getPlatformId() {
		return platformId;
	}

	public void setPlatformId(int platformId) {
		this.platform = Platform.getType(platformId);
		this.platformId = platformId;
	}

	public Platform getPlatform() {
		return platform;
	}

	public void setPlatform(Platform platform) {
		this.platform = platform;
	}

	public List<Permission> getChildren() {
		return children;
	}

	public void setChildren(List<Permission> children) {
		this.children = children;
	}

	public void addToChildren(Permission permission) {
		if (this.children == null) {
			this.children = new ArrayList<Permission>();
		}
		this.children.add(permission);
	}

	public boolean isChecked() {
		return checked;
	}

	public void setChecked(boolean checked) {
		this.checked = checked;
	}

	public int getDataScopeId() {
		return dataScopeId;
	}

	public void setDataScopeId(int dataScopeId) {
		this.dataScopeId = dataScopeId;
	}

	public String getPermUrl() {
		if (permUrl == null) {
			permUrl = controller;
		}
		return permUrl;
	}

	@Override
	public String toString() {
		return "Permission [id=" + id + ", displayName=" + name
				+ ", controller=" + controller + ", parentID=" + parentId
				+ ", isMenu=" + isMenu + ", ico_cls=" + ico_cls
				+ ", level=" + level + ", dataScope=" + dataScopes
				+ ", platform=" + platform + ", checked=" + checked + "]";
	}

	@Override
	public String toJSON() {
		StringBuilder sb = new StringBuilder();
		sb.append("{\"id\":" + id);
		sb.append(",\"displayName\":\"" + name + "\"");
		sb.append(",\"controller\":\"" + controller + "\"");
		sb.append(",\"parentId\":" + parentId);
		sb.append(",\"isMenu\":" + isMenu);
		sb.append(",\"ico_cls\":\"" + ico_cls + "\"");
		sb.append(",\"level\":" + level);
		sb.append(",\"permUrl\":\"" + permUrl + "\"");
		sb.append(",\"dataScopes\":\"" + dataScopes + "\"");
		sb.append(",\"platformId\":" + platform.getIndex());
		sb.append(",\"platformName\":\"" + platform.name() + "\"");
		if (children != null && children.size() > 0) {
			sb.append(",\"children\":[");
			int i = 0;
			for (Permission c : children) {
				sb.append((i++) > 0 ? "," : "");
				sb.append(c.toJSON());
			}
			sb.append("]");
		} else {
			sb.append(",\"children\":null");
		}
		sb.append(",\"checked\":" + checked);
		sb.append(",\"dataScopeId\":" + dataScopeId);
		sb.append("}");

		return sb.toString();
	}
}
